package com.numberma.permissionservice.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.numberma.permissionservice.Result.Result;
import com.numberma.permissionservice.constant.RoleConstant;
import com.numberma.permissionservice.constant.RoleNameConstant;
import com.numberma.permissionservice.entity.Role;

import com.numberma.permissionservice.entity.UserRoles;
import com.numberma.permissionservice.mapper.PermissionMapper;
import com.numberma.permissionservice.mapper.RoleMapper;
import com.numberma.permissionservice.service.PermissionService;

import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@GlobalTransactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper  permissionMapper;
    @Autowired
    private RoleMapper  roleMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Result bindDefaultRole(Long userId) {
        //可以使用AOP
        UserRoles  userRole = new UserRoles();
        userRole.setUserId(userId);
        userRole.setRoleId(RoleConstant.USER);
        permissionMapper.insertUserRole(userRole);

        return Result.ok();

    }

    @Override
    public String getUserRole(Long userId) {
       UserRoles  userRole =  permissionMapper.selectByUserId(userId);
       log.info("用户身份:{}",userRole);
       if(userRole == null){
            return "用户不存在";
       }

        if(redisTemplate.opsForValue().get(userRole.getRoleId()) == null){
           Role  role = roleMapper.selectByRoleId(userRole.getRoleId());
           String roleCode = role.getRoleCode();
           redisTemplate.opsForValue().set(userRole.getRoleId(),roleCode);
        }
         return  redisTemplate.opsForValue().get(userRole.getRoleId()).toString() ;
    }

    @Override
    public void upgradeToAdmin(Long userId) {
        Integer AdMinRoleId = (Integer) redisTemplate.opsForValue().get(RoleNameConstant.ADMIN);

        if(AdMinRoleId == 0||AdMinRoleId == null){
             AdMinRoleId = roleMapper.GetIdByRoleCode(RoleNameConstant.ADMIN);

            redisTemplate.opsForValue().set(RoleNameConstant.ADMIN,AdMinRoleId);
        }


        permissionMapper.upgradeToAdmin(userId,AdMinRoleId);
    }

    @Override
    public void downgradeToUser(Long userId) {
        int AdMinRoleId = (int) redisTemplate.opsForValue().get(RoleNameConstant.ADMIN);
        int UserRoleId = (int) redisTemplate.opsForValue().get(RoleNameConstant.USER);
        if(AdMinRoleId == 0){
            int RoleId = roleMapper.GetIdByRoleCode(RoleNameConstant.ADMIN);

            redisTemplate.opsForValue().set(RoleNameConstant.ADMIN,RoleId);
        }
        if(UserRoleId == 0){
            int RoleId = roleMapper.GetIdByRoleCode(RoleNameConstant.USER);
            redisTemplate.opsForValue().set(RoleNameConstant.USER,RoleId);
        }
        UpdateWrapper<UserRoles> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId).set("role_id", RoleNameConstant.ADMIN);

        permissionMapper.update(null, updateWrapper);

    }

    @Override
    public List<Integer> getUserIdsByRoleCode(String roleCode) {

        int RoleId = (int) redisTemplate.opsForValue().get(roleCode);
        if(RoleId == 0){
           RoleId = roleMapper.GetIdByRoleCode(roleCode);

            redisTemplate.opsForValue().set(roleCode,RoleId);
        }

        return permissionMapper.getUserIdByRoleId(RoleId);
    }
}
